<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/16
 * Time: 14:03
 */

namespace Admin\Controller;


use Admin\Model\Page;
use Common\Utils\ImageUtils;

class GoodsController extends BaseController
{
    private static $_validate = array(
        array('title', 'require', '商品名称不能为空！', 1, 'regex', 3),
        array('describe', 'require', '商品简单描述不能为空！', 1, 'regex', 3),
        array('image_url', 'require', '商品图片不能为空！', 1, 'regex', 3),
        array('poundage', 'require', '手续费不能为空！', 1, 'regex', 3),
        array('money', 'require', '价格不能为空！', 1, 'regex', 3),
    );

    public function __construct($baseModel)
    {
        parent::__construct(D('Goods'));
        $this->bcItemPush('商品列表', U('Goods/index'));
    }
    public function index()
    {
        $model = D('Goods');
        //构建查询条件
        $pagesize = 20;
        $title = I('title', '', 'htmlspecialchars');
        $id = I('id', '', 'htmlspecialchars');
        $pagenumber = I('pagenum', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($title != '') {
            $map['t_goods.title'] = array('like', '%' . $title . '%');
        }
        if(!empty($id)){
            $map['t_goods.id'] = $id;
        }
        $map['is_delete'] = 0;
        //查询
        $list = $model->getByConditionPage($map, $pagenumber, $pagesize);
        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组
        //显示数据
        $this->assign('page', $page);
        $this->assign('title', $title);
        $this->assign('id', $id);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->display();
    }

    /**
     * 添加
     */
    public function addUI()
    {
        $this->bcItemPush('添加商品', U('Goods/addUI'));
        $this->display('save');
    }

    /**
     * 编辑
     * @param $id 图片id
     */
    public function editUI($id)
    {
        $map['id'] = ':id';
        $bind[':id'] = $id;
        $goods = M('Goods')->where($map)->bind($bind)->find();
        $this->bcItemPush('编辑商品', U('Goods/editUI'));
        $this->assign('goods', $goods);
        $this->display('save');
    }

    public function doEditBefore($data)
    {
        $goods = $this->baseModel->find($data['id']);
        if (file_exists($goods['image_url']) && $goods['image_url'] != $data['image_url'])
            unlink('./' . $goods['image_url']);
    }

    public function edit()
    {
        parent::edit($this::$_validate);
    }

    public function doEditAfter($data, $success)
    {
//        $data['create_time'] = time();
//        D('Goods')->save($data);
        $utils = new ImageUtils();
        $utils->thum_images($data['id'],180,180,'goods',$data['image_url'],true);//重新生成缩略图
    }

    //添加图文详情
    public function add()
    {
        parent::add($this::$_validate);

    }

    public function delete($id){
        $goods = D('Goods')->where(array('id'=>$id))->find();
        if(!$goods || $goods['is_delete'] == 1){
            $this->ajaxReturn(array('success'=>false,'message'=>'该商品不能被删除！'));
        }
        D('Goods')->where(array('id'=>$id))->setField('is_delete',1);
        $count = D('Goods')->where(array('is_delete'=>0))->count();
        $this->ajaxReturn(array('success'=>true,'message'=>'删除成功！','count'=>$count));
    }

    public function updateField()
    {
        $id = I('id');
        $value = I('value');
        $field = I('field');
        $where['id'] = ':id';
        $bind[':id'] = $id;
        $category = D('Goods')->where($where)->bind($bind)->find();
        $category[$field] = $value;
        $result = D('Goods')->where($where)->bind($bind)->setField($field,$value);
        if($result){
            $this->ajaxReturn(array('success'=>true,'message'=>'修改成功'));
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'修改失败'.D('Goods')->getLastSql()));
        }
    }
}